package Map集合;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class TestMap {
    public static void main(String[] args) {
        Map<Integer,String> map = new HashMap();
        map.put(1,"我");
        map.put(2,"你");
        map.put(3,"她");

        System.out.println(map);//{1=我, 2=你, 3=她}   注意外面是大括号，里面是key=value

        System.out.println("---------遍历方式一：采用keySet()-------");
        Set<Integer> keySet = map.keySet();
        Iterator<Integer> it = keySet.iterator();
        while(it.hasNext()){
            Integer key = it.next();
            String value = map.get(key);//map.get(key)
            System.out.println("{"+key+"="+value+"}");//换行打印
        }

        System.out.println("------遍历方式二：采用entrySet()-----------");
        Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
        Iterator<Map.Entry<Integer, String>> it2 = entrySet.iterator();
        while(it2.hasNext()){
            Map.Entry<Integer, String> entry = it2.next();
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println("{"+key+"="+value+"}");
        }

    }
}
